/*
Copyright (c) 2007 Bill Davidsen (wdavidsen@yahoo.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.Design;

using SCS.Web.Controls;

namespace SCS.Web.Controls.Design
{
	/// <summary>
	/// Summary description for MenuControlDesigner.
	/// </summary>
	public class MenuControlDesigner : ControlDesigner
	{
		public MenuControlDesigner()
		{
		}
		
		public override void Initialize(System.ComponentModel.IComponent component)
		{
			if (! (component is MultiMenu) && !(component is INamingContainer))
			{
				throw new ArgumentException("Component must be a the MultiMenu control.", "component");
			}

			base.Initialize (component);
		}

        public override string GetDesignTimeHtml()
        {
            string html = string.Empty;

            MultiMenu menu = (MultiMenu)Component;

            try
            {
                if (menu.MultiMenuType != MenuType.Context)
                {
                    StringWriter stringWriter = new StringWriter();
                    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

                    menu.WriteMenu(htmlWriter);

                    html = stringWriter.ToString();

                    if (string.IsNullOrEmpty(html))
                    {
                        html = this.GetEmptyDesignTimeHtml();
                    }
                }
                else
                {
                    html += "<div style=\"font-family:arial;font-weight:normal;color:#000000;font-size:11px;border:solid 1px #989898;padding:3px;background-color:#F1F1F1;\">";
                    html += "MultiMenu: \"" + this.ID + "\"</div>";
                }
            }
            catch (Exception ex)
            {
                html = this.GetErrorDesignTimeHtml(ex);
            }
            finally
            {
                //menu.Dispose();
            }

            return html;
        }
		protected override string GetErrorDesignTimeHtml(Exception e)
		{
			return base.GetErrorDesignTimeHtml(e);
		}
		protected override string GetEmptyDesignTimeHtml()
		{
            string html = string.Empty;

            MultiMenu menu = (MultiMenu)Component;

            try
            {
                if (menu.MultiMenuType != MenuType.Context)
                {
                    MultiMenuItem item = new MultiMenuItem("Item 1");
                    menu.TopMenuGroup.MultiMenuItems.Add(item);

                    if (menu.MultiMenuType == MenuType.Horizontal)
                    {
                        MultiMenuItem divider = new MultiMenuItem();
                        divider.CssClass = " ";
                        divider.IsDivider = true;

                        menu.TopMenuGroup.MultiMenuItems.Add(divider);

                        MultiMenuItem item2 = new MultiMenuItem("Item 2");
                        menu.TopMenuGroup.MultiMenuItems.Add(item2);

                        menu.TopMenuGroup.MultiMenuItems.Add(divider);

                        MultiMenuItem item3 = new MultiMenuItem("Item 3");
                        menu.TopMenuGroup.MultiMenuItems.Add(item3);
                    }

                    // Set classes
                    menu.LevelGroupClasses.Add(new GroupClass("TopMenuGroup"));
                    menu.LevelItemClasses.Add(new ItemClass("MenuItem", "MenuItemOver", "MenuItemSelected"));

                    StringWriter stringWriter = new StringWriter();
                    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

                    menu.WriteMenu(htmlWriter);

                    html = stringWriter.ToString();

                    //html += "<br>";
                    //html += menu.DesignDebugInfo;
                }
            }
            catch (Exception ex)
            {
                html = this.GetErrorDesignTimeHtml(ex);                
            }
            finally
            {
                //menu.Dispose();
            }
            
            return html;
		}
	}
}
